package dk.marten.ibdrecord;

import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class ToiletHistory extends Activity {	

	private DBAccess datasource;
	private HistoryListAdaptor<ToiletData> adapter;
	private ListView listView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_toilet_history);

		
		listView = (ListView) findViewById(R.id.historyList);
		
		datasource = new DBAccess(this);
//	    datasource.open();
	    
		List<ToiletData> values = datasource.getAllToiletData();

	    adapter = new HistoryListAdaptor<ToiletData>(this, R.layout.history_list_item, values);
	    
	    listView.setAdapter(adapter); 
	    
	    final Activity that = this;
	    
	    listView.setOnItemClickListener(new OnItemClickListener() {
	    	  @Override
	    	  public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
	    		ToiletData t = adapter.getItem(position);

		   		Intent intent = new Intent(that, RedigerToilet.class);
		   		intent.putExtra("id",t.getId()); 
		   		startActivity(intent);
	    	  }
	    	}); 

	    datasource.close();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_toilet_history, menu);
		return true;
	}
	
	  @Override
	  protected void onResume() {
	    datasource.open();
	    List<ToiletData> values = datasource.getAllToiletData();
	    adapter = new HistoryListAdaptor<ToiletData>(this, R.layout.history_list_item, values);
	    listView.setAdapter(adapter); 
	    datasource.close();
	    super.onResume();
	  }

	  @Override
	  protected void onPause() {
	    super.onPause();
	  }

}
